﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using System.Collections.ObjectModel;

namespace Np.GalleryDownloader.Classes
{
    public class DownloadPage : INotifyPropertyChanged
    {
        #region Fields
        string link;
        DateTime added;
        DateTime endDownload;
        int numberOfImages;
        ObservableCollection<DownloadImage> imageLinks;
        private List<DownloadImage> downloadedImages = new List<DownloadImage>();
        Stack<DownloadImage> imagesToDownload = new Stack<DownloadImage>();
        string siteImageRoot;
        string siteRoot;
        bool isInit = false;
        #endregion

        #region Properties
        public DateTime EndDownload
        {
            get { return endDownload; }
            set
            {
                endDownload = value;
                this.NotifyPropertyChanged("EndDownload");
            }
        }
        public DateTime Added
        {
            get { return added; }
            set
            {
                added = value;
                this.NotifyPropertyChanged("Added");

            }
        }
        public string Link
        {
            get { return link; }
            internal set
            {
                link = value;
                this.NotifyPropertyChanged("Link");
            }
        }

        public int NumberOfImages
        {
            get { return numberOfImages; }
            set
            {
                numberOfImages = value;
                this.NotifyPropertyChanged("NumberOfImages");
            }
        }

        public ObservableCollection<DownloadImage> ImageLinks
        {
            get { return imageLinks; }
            set
            {
                imageLinks = value;
                this.NotifyPropertyChanged("ImageLinks");
            }
        }


        public string SiteImageRoot
        {
            get { return siteImageRoot; }
            set
            {
                siteImageRoot = value;
                this.NotifyPropertyChanged("SiteImageRoot");
            }
        }

        public string SiteRoot
        {
            get { return siteRoot; }
            set { siteRoot = value; }
        }


        public bool IsInit
        {
            get { return isInit; }
            set { isInit = value; }
        }

        public List<DownloadImage> DownloadedImages
        {
            get { return downloadedImages; }
            set { downloadedImages = value; }
        }

        #endregion

        private void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        public DownloadPage(string pageLink, bool? isFolderPage = false)
        {
            Link = pageLink;
            Added = DateTime.Now;
            try
            {
                ImageLinks = new ObservableCollection<DownloadImage>();
                SiteImageRoot = ResolveSiteImageRoot();
                SiteRoot = ResolveSiteRoot();
                FetchImages();
                IsInit = true;
            }
            catch { }
        }


        private string ResolveSiteImageRoot()
        {
            Uri u = new Uri(Link);
            string sRoot = u.DnsSafeHost;
            foreach (var seg in u.Segments)
            {
                if (seg.Contains(@"/"))
                    sRoot += seg;
            }
            return sRoot;
        }

        private string ResolveSiteRoot()
        {
            Uri u = new Uri(Link);
            return u.DnsSafeHost;
        }

        private void FetchImages()
        {
            HtmlWeb web = new HtmlWeb();
            HtmlDocument doc = web.Load(Link);

            var allLinksNodes = doc.DocumentNode.SelectNodes(@"//a[@href]");
            var allImageLinkNodes = (from l in allLinksNodes
                                     where l.Attributes["href"].Value.ToLower().EndsWith(".jpg")
                                     || l.Attributes["href"].Value.ToLower().EndsWith(".jpeg")
                                     || l.Attributes["href"].Value.ToLower().EndsWith(".png")
                                     || l.Attributes["href"].Value.ToLower().EndsWith(".gif")
                                     select l);

            foreach (var lnk in allImageLinkNodes)
            {
                var attr = lnk.Attributes["href"].Value;
                var dImg = new DownloadImage(this, attr);
                imageLinks.Add(dImg);
                imagesToDownload.Push(dImg);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
